import CryptoJS from 'crypto-js';

// 生成 WebSocket 鉴权 URL（星火 API 需要）
export const getWebSocketUrl = (): Promise<string> => {
  return new Promise((resolve) => {
    const apiKey = "c4cc84326174d43ec64505644cdb86a2";
    const apiSecret = "NTAxZDcxY2Y5YTk5YzE0ZGI5MzUyYmM2";
    const host = location.host;
    const date = new Date().toUTCString();
    const algorithm = 'hmac-sha256';
    const headers = 'host date request-line';
    const signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2.1/chat HTTP/1.1`;
    
    const signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret);
    const signature = CryptoJS.enc.Base64.stringify(signatureSha);
    
    const authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`;
    const authorization = btoa(authorizationOrigin);
    
    resolve(`wss://spark-api.xf-yun.com/v2.1/chat?authorization=${authorization}&date=${date}&host=${host}`);
  });
};